package com.wangzhe.builder;

import com.wangzhe.bean.Constants;
import com.wangzhe.bean.FieldInfo;
import com.wangzhe.bean.TableInfo;
import com.wangzhe.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * @author 王哲
 * @ClassName BuilderService
 * @create 2023--六月--下午2:53
 * @Description 构造Controller类
 * @Version V1.0
 */
public class BuilderController {
    private static Logger logger = LoggerFactory.getLogger(BuilderController.class);

    public static void builderController(TableInfo tableInfo) {
        File folder = new File(Constants.PACKAGE_CONTROLLER_PATH);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        File file = new File(folder, tableInfo.getBeanName() + "Controller.java");

        OutputStream os = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;

        try {
            os = new FileOutputStream(file);
            osw = new OutputStreamWriter(os, "utf-8");
            bw = new BufferedWriter(osw);

            bw.write("package " + Constants.PACKAGE_CONTROLLER + "; ");
            bw.newLine();
            bw.newLine();
            bw.write("import java.util.List;");
            bw.newLine();
            bw.write("import " + Constants.POJO_BASE + "." + tableInfo.getBeanName() + ";");
            bw.newLine();
            bw.write("import " + Constants.EXAMPLE_PATH + "." + tableInfo.getBeanParamName() + ";");
            bw.newLine();
            String serviceName = tableInfo.getBeanName() + "Service";
            bw.write("import " + Constants.PACKAGE_SERVICE + "." + serviceName + ";");
            bw.newLine();
            bw.write("import " + Constants.VO_PATH + ".ResponseVO;");
            bw.newLine();
            bw.write("import org.springframework.web.bind.annotation.*;");
            bw.newLine();
            bw.write("import javax.annotation.Resource;");
            bw.newLine();

            String beanName = tableInfo.getBeanName() + "Controller";
            bw.newLine();

            BuilderComment.createClassComment(bw, beanName + "对应数据库表" + tableInfo.getTableName() + "的Controller类");
            //controller注解
            bw.write("@RestController");
            bw.newLine();
            bw.write("@RequestMapping(\"/" + StringUtils.lowerCaseFirst(tableInfo.getBeanName()) + "\")");
            bw.newLine();
            bw.write("public class " + beanName + " extends BaseController {");
            bw.newLine();
            bw.newLine();
            bw.write("\t@Resource");
            bw.newLine();
            bw.write("\tprivate " + serviceName + " " + StringUtils.lowerCaseFirst(serviceName) + ";");
            bw.newLine();
            bw.newLine();
            BuilderComment.createMethodComment(bw, "加载数据列表");
            bw.write("\t@RequestMapping(\"/list\")");
            bw.newLine();
            bw.write("\tpublic ResponseVO" + " findListByPage(" + tableInfo.getBeanParamName() + " query) {");
            bw.newLine();
            bw.write("\t\treturn getSuccessResponseVO(this." + StringUtils.lowerCaseFirst(serviceName) + ".findListByPage(query));");
            bw.newLine();
            bw.write("\t}");
            bw.newLine();
            bw.newLine();
            BuilderComment.createMethodComment(bw, "新增");
            bw.write("\t@RequestMapping(\"/add\")");
            bw.newLine();
            bw.write("\tpublic ResponseVO add(" + tableInfo.getBeanName() + " bean ) {");
            bw.newLine();
            bw.newLine();
            bw.write("\t\tthis." + StringUtils.lowerCaseFirst(serviceName) + ".add(bean);");
            bw.newLine();
            bw.newLine();
            bw.write("\t\treturn getSuccessResponseVO(null);");
            bw.newLine();
            bw.write("\t}");
            bw.newLine();
            bw.newLine();
            BuilderComment.createMethodComment(bw, "批量新增");
            bw.write("\t@RequestMapping(\"/addBatch\")");
            bw.newLine();
            bw.write("\tpublic ResponseVO addBatch(@RequestBody List<" + tableInfo.getBeanName() + "> listBean) {");
            bw.newLine();
            bw.newLine();
            bw.write("\t\tthis." + StringUtils.lowerCaseFirst(serviceName) + ".addBatch(listBean);");
            bw.newLine();
            bw.newLine();
            bw.write("\t\treturn getSuccessResponseVO(null);");
            bw.newLine();
            bw.write("\t}");
            bw.newLine();
            bw.newLine();
            BuilderComment.createMethodComment(bw, "批量新增/修改");
            bw.write("\t@RequestMapping(\"/addOrUpdateBatch\")");
            bw.newLine();
            bw.write("\tpublic ResponseVO addOrUpdateBatch(@RequestBody List<" + tableInfo.getBeanName() + "> listBean) {");
            bw.newLine();
            bw.newLine();
            bw.write("\t\tthis." + StringUtils.lowerCaseFirst(serviceName) + ".addOrUpdateBatch(listBean);");
            bw.newLine();
            bw.newLine();
            bw.write("\t\treturn getSuccessResponseVO(null);");
            bw.newLine();
            bw.write("\t}");
            bw.newLine();
            bw.newLine();

            //拿到主键
            Map<String, List<FieldInfo>> keyIndexMap = tableInfo.getKeyIndexMap();
            for (Map.Entry<String, List<FieldInfo>> stringListEntry : keyIndexMap.entrySet()) {
                List<FieldInfo> value = stringListEntry.getValue();

                Integer index = 0;
                StringBuilder sb = new StringBuilder();
                StringBuilder methodParams = new StringBuilder();
                StringBuilder mapperMethodParams = new StringBuilder();

                for (FieldInfo fieldInfo : value) {
                    index++;
                    sb.append(StringUtils.upperCaseFirst(fieldInfo.getPropertyName()));
                    if (index < value.size()) {
                        sb.append("And");
                    }
                    methodParams.append(fieldInfo.getFieldJavaType() + " " + fieldInfo.getPropertyName());
                    mapperMethodParams.append(fieldInfo.getPropertyName());
                    if (index < value.size()) {
                        methodParams.append(",");
                        mapperMethodParams.append(",");
                    }
                }
                //查询语句生成
                BuilderComment.createMethodComment(bw, "根据" + sb.toString() + "查询");
                bw.write("\t@RequestMapping(\"/get" + tableInfo.getBeanName() + "By" + sb.toString() +"\")");
                bw.newLine();
                bw.write("\tpublic ResponseVO get" + tableInfo.getBeanName() + "By" + sb.toString() + "(" + methodParams + ") {");
                bw.newLine();
                bw.newLine();

                bw.write("\t\treturn getSuccessResponseVO(this." + StringUtils.lowerCaseFirst(serviceName) + ".get" + tableInfo.getBeanName() + "By" + sb.toString() + "(" + mapperMethodParams + "));");
                bw.newLine();

                bw.write("\t}");
                bw.newLine();
                bw.newLine();
                //删除语句生成
                BuilderComment.createMethodComment(bw, "根据" + sb.toString() + "删除");
                bw.write("\t@RequestMapping(\"/delete" + tableInfo.getBeanName() + "By" + sb.toString() +"\")");
                bw.newLine();
                bw.write("\tpublic ResponseVO delete" + tableInfo.getBeanName() + "By" + sb.toString() + "(" + methodParams + ") {");
                bw.newLine();
                bw.newLine();
                bw.write("\t\tthis." + StringUtils.lowerCaseFirst(serviceName) + ".delete" + tableInfo.getBeanName() + "By" + sb.toString() + "(" + mapperMethodParams + ");");
                bw.newLine();
                bw.newLine();
                bw.write("\t\treturn getSuccessResponseVO(null);");
                bw.newLine();
                bw.write("\t}");
                bw.newLine();
                bw.newLine();
                //更新语句生成
                BuilderComment.createMethodComment(bw, "根据" + sb.toString() + "更新");
                bw.write("\t@RequestMapping(\"/update" + tableInfo.getBeanName() + "By" + sb.toString() +"\")");
                bw.newLine();
                bw.write("\tpublic ResponseVO update" + tableInfo.getBeanName() + "By" + sb.toString() + "( " + tableInfo.getBeanName() + " bean, " + methodParams + ") {");
                bw.newLine();
                bw.newLine();
                bw.write("\t\tthis." + StringUtils.lowerCaseFirst(serviceName) + ".update" + tableInfo.getBeanName() + "By" + sb.toString() + "(bean," + mapperMethodParams + ");");
                bw.newLine();
                bw.newLine();
                bw.write("\t\treturn getSuccessResponseVO(null);");
                bw.newLine();
                bw.write("\t}");
                bw.newLine();
                bw.newLine();
            }

            bw.write("}");
            bw.newLine();
            bw.newLine();

        } catch (Exception e) {
            logger.info("创建serviceImpl失败:{}", e.getMessage());
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
                if (osw != null) {
                    osw.close();
                }
                if (os != null) {
                    os.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
